﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// ZipHelper 的摘要说明
/// </summary>
public class ZipHelper
{
    public ZipHelper()
    {

    }

    /// <summary>  
    /// 制作压缩包（多个文件压缩到一个压缩包，支持加密、注释）  
    /// </summary>  
    /// <param name="topDirectoryName">压缩文件目录</param>  
    /// <param name="zipedFileName">压缩包文件名</param>  
    /// <param name="filter">筛选的文件格式后缀名,png,jpg等</param>  
    /// <param name="compresssionLevel">压缩级别 1-9</param>  
    /// <param name="password">密码</param>  
    /// <param name="comment">注释</param>  
    public static void ZipFiles(string topDirectoryName, string zipedFileName, string filter = "png", int compresssionLevel = 0, string password = "", string comment = "")
    {
        using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate)))
        {
            if (compresssionLevel != 0)
            {
                zos.SetLevel(compresssionLevel);//设置压缩级别  
            }

            if (!string.IsNullOrEmpty(password))
            {
                zos.Password = password;//设置zip包加密密码  
            }

            if (!string.IsNullOrEmpty(comment))
            {
                zos.SetComment(comment);//设置zip包的注释  
            }

            //循环设置目录下所有的*.jpg文件（支持子目录搜索）  
            foreach (string file in Directory.GetFiles(topDirectoryName, "*." + filter, SearchOption.TopDirectoryOnly))
            {
                if (File.Exists(file))
                {
                    FileInfo item = new FileInfo(file);
                    FileStream fs = File.OpenRead(item.FullName);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);

                    ZipEntry entry = new ZipEntry(item.Name);
                    zos.PutNextEntry(entry);
                    zos.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }

    /// <summary>  
    /// 解压缩包（将压缩包解压到指定目录）  
    /// </summary>  
    /// <param name="zipedFileName">压缩包名称</param>  
    /// <param name="unZipDirectory">解压缩目录</param>  
    /// <param name="password">密码</param>  
    public static void UnZipFiles(string zipedFileName, string unZipDirectory, string password = "")
    {
        using (ZipInputStream zis = new ZipInputStream(File.Open(zipedFileName, FileMode.OpenOrCreate)))
        {
            if (!string.IsNullOrEmpty(password))
            {
                zis.Password = password;//有加密文件的，可以设置密码解压  
            }

            ZipEntry zipEntry;
            while ((zipEntry = zis.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(unZipDirectory);
                string pathName = Path.GetDirectoryName(zipEntry.Name);
                string fileName = Path.GetFileName(zipEntry.Name);

                pathName = pathName.Replace(".", "$");
                directoryName += "\\" + pathName;

                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }

                if (!string.IsNullOrEmpty(fileName))
                {
                    FileStream fs = File.Create(Path.Combine(directoryName, fileName));
                    int size = 2048;
                    byte[] bytes = new byte[2048];
                    while (true)
                    {
                        size = zis.Read(bytes, 0, bytes.Length);
                        if (size > 0)
                        {
                            fs.Write(bytes, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    fs.Close();
                }
            }
        }
    }
}